import mylibs
import webapp2
import lxml
from unidecode import unidecode
from google.appengine.api import urlfetch
from lxml import etree, html
from lxml.html.clean import Cleaner
    
basurl = 'http://baskino.com/'
cleaner = Cleaner(remove_tags=['li', 'em', 'span', 'div'], remove_unknown_tags=False)

def get_page(url):
    "get url"
    website = urlfetch.fetch(url)
    return html.fromstring(str(website.content))

class categories(webapp2.RequestHandler):
    def get(self):
        root = get_page(basurl)

        categories=etree.Element("categories")
        for element in root.cssselect("ul.sf-menu li"):
            el=cleaner.clean_html(element)
            #el.make_links_absolute(basurl)
            category_url=el[0].attrib['href']

            category_root=get_page(basurl)
            #text=el[0].text
            text=unidecode(el[0].text).replace("'","")

            category=etree.SubElement(categories, "category")
            #text=el[0].text
            category.set('title', text)
            category.set('description', "")
            category_element=category_root.cssselect("div.postcover a")
            for e in category_element:
                 category.set('sd_img', str(e[0].attrib['src']))
                 category.set('hd_img', str(e[0].attrib['src']))
                 break
            #category.set('debug', str(e[0].attrib['src']))
            categoryLeaf=etree.SubElement(category, "categoryLeaf")
            categoryLeaf.set('title', text)
            categoryLeaf.set('description', "")
            categoryLeaf.set('feed', category_url)
            
        self.response.headers['Content-Type'] = 'text/xml'
        self.response.write(etree.tostring(categories, pretty_print=True, xml_declaration=True, encoding='UTF-8', standalone="yes"))

class feeds(webapp2.RequestHandler):
    def get(self):
        root=get_page(basurl+self.request.path)
        feed=etree.Element("feed")
        rl=etree.SubElement(feed, "resultLength")
        dbg=etree.SubElement(feed, "debug")
        i=0
        for element in root.cssselect("div.postcover a"):
            i+=1
            el=cleaner.clean_html(element)
            item=etree.SubElement(feed, "item")
            item.set('sdImg', (el[0].attrib['src']))
            item.set('hdImg', (el[0].attrib['src']))
            title=etree.SubElement(item, "title")
            title_text=unidecode(el[0].attrib['title'])
            title.text=title_text#.replace("'","")
            media=etree.SubElement(item, "media")
            streamurl=etree.SubElement(media, "streamUrl")
            streamurl.text=el.attrib['href']
            streamqlty=etree.SubElement(media, "streamQuality")
            streamqlty.text="SD"
            streamfrmt=etree.SubElement(media, "streamFormat")
            streamfrmt.text="mp4"
            synops=etree.SubElement(item, "synopsis")
            synops.text=el[0].attrib['alt']
            genres=etree.SubElement(item, "genres")
            genres.text="Movie"
        rl.text=str(i)
        #dbg.text=str(html.tostring(root))

        self.response.headers['Content-Type'] = 'text/xml'
        self.response.write(etree.tostring(feed, pretty_print=True, xml_declaration=True, encoding='UTF-8', standalone="yes"))
        #self.response.write(self.request.url+" "+self.request.path)

#app = webapp2.WSGIApplication([('/*', MainPage)], debug=True)
app = webapp2.WSGIApplication([ \
                               ('/categories.xml', categories), \
                               ('/films/.*', feeds) \
                               ], debug=True)
